package com.myapp.mobilesafe.service;

import android.app.ActivityManager;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Handler;
import android.os.IBinder;
import android.os.SystemClock;
import android.util.Log;

import com.myapp.mobilesafe.activity.EnterPwdActivity;
import com.myapp.mobilesafe.db.dao.AppLockDao;

import java.util.ArrayList;
import java.util.List;

public class WatchDogService extends Service {

    private ActivityManager mAM;
    private Boolean isRunning = true;
    private AppLockDao mDao;
    private MyReceiver mReceiver;
    private String mSkipPackage;
    private ArrayList<String> mLockList;

    private MyContentObserver mObserver;
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        mDao = AppLockDao.getInstance(this);
        mAM = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
        mLockList = mDao.findAll();//处理并发
        new Thread() {
            @Override
            public void run() {
                while (isRunning) {
                    List<ActivityManager.RunningTaskInfo> runningTask = mAM.getRunningTasks(1);
                    String packageName = runningTask.get(0)
                            .topActivity.getPackageName();
                    if (mLockList.contains(packageName)
                            && !packageName.equals(mSkipPackage)) {
                        Intent intent = new Intent(getApplicationContext(),
                                EnterPwdActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent.putExtra("packageName",packageName);
                        startActivity(intent);
                    }
                    //Log.i("hahah",packageName+"@!@");
                    SystemClock.sleep(100);
                }
            }
        }.start();
        mReceiver = new MyReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction("com.myapp.mobilesafe.SKIP_CHECK");
        registerReceiver(mReceiver, filter);
        mObserver = new MyContentObserver(null);
        getContentResolver().registerContentObserver(
                Uri.parse("content://com.myapp.mobilesafe/change"), true,
                mObserver);
    }

    @Override
    public void onDestroy() {
        isRunning = false;
        unregisterReceiver(mReceiver);
        mReceiver = null;
        getContentResolver().unregisterContentObserver(mObserver);
        mObserver = null;
    }
    class MyReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            mSkipPackage = intent.getStringExtra("package");
        }
    }
    class MyContentObserver extends ContentObserver {//内容观察者。

        public MyContentObserver(Handler handler) {
            super(handler);
        }

        @Override
        public void onChange(boolean selfChange) {
            super.onChange(selfChange);
            // 重新拉取数据库最新数据
            mLockList = mDao.findAll();// 查询所有已加锁应用集合
        }
    }
}
